package com.hzsk.lisence.controller;

import com.hzsk.lisence.config.myexception.BaseException;
import com.hzsk.lisence.config.myexception.ParameterException;
import com.hzsk.lisence.entity.LisenceUserEntity;
import com.hzsk.lisence.entity.request.UkeyRequest;
import com.hzsk.lisence.entity.response.IncResponce;
import com.hzsk.lisence.enums.UserRoleEnum;
import com.hzsk.lisence.mapper.LisenceUserMapper;
import com.hzsk.lisence.service.EmUkeyService;
import com.hzsk.lisence.service.UserService;
import com.hzsk.lisence.util.EncipherUtils;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.UnsupportedEncodingException;

/**
 * @Description : U盾制作接口
 * @Date : 2024/8/13
 * @Author : ChenJin
 * @Version : v1.0.0
 **/
@CrossOrigin
@RestController
@RequestMapping("/api/lisence/ukey")
public class EmUkeyController {
    
    @Resource
    private EmUkeyService ukeyService;
    
    @Resource
    private LisenceUserMapper lisenceUserMapper;
    
    @PostMapping("/page")
    @ApiOperation(value = "分页查询生产制作锁")
    public IncResponce getUkeyPage(@RequestBody UkeyRequest ukeyRequest) {
        IncResponce incResponce = new IncResponce();
        try {
            incResponce.setData(ukeyService.getUkeyPage(ukeyRequest));
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            incResponce.setErrorMessage(e.getMessage());
            e.printStackTrace();
        }
        return incResponce;
    }
    
    @PostMapping("/verify")
    @ApiOperation(value = "制作U盾账号验证")
    public IncResponce verifyUser(@RequestBody UkeyRequest ukeyRequest){
        IncResponce incResponce = new IncResponce();
        try {
            if (ObjectUtils.isEmpty(ukeyRequest) ||
                    StringUtils.isEmpty(ukeyRequest.getUserAccount()) || 
                    StringUtils.isEmpty(ukeyRequest.getUserPassword())) {
                throw new ParameterException("请求参数异常");
            }
            LisenceUserEntity lisenceUserEntity = new LisenceUserEntity();
            lisenceUserEntity.setUserAccount(ukeyRequest.getUserAccount());
            LisenceUserEntity entity = lisenceUserMapper.selectEntityByAccountAndPassword(lisenceUserEntity);
            if (ObjectUtils.isEmpty(entity)){
                throw new BaseException("账号密码错误！");
            }
            String decrypt = EncipherUtils.decrypt(entity.getPassword());
            if (!ukeyRequest.getUserPassword().equals(decrypt)){
                throw new BaseException("账号密码错误！");
            }
            if (!(UserRoleEnum.PRODUCE_LEADER.getCode().equalsIgnoreCase(entity.getUserRole())||
                    UserRoleEnum.PRODUCE_MANAGER.getCode().equalsIgnoreCase(entity.getUserRole()))){
                throw new BaseException("账号无权限！");
            }
            incResponce.setData(entity);
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            if (StringUtils.isNotEmpty(e.getMessage())){
                try {
                    byte[] utf8Bytes  = e.getMessage().getBytes("GB2312");
                    String utf8String = new String(utf8Bytes, "GB2312");
                    incResponce.setErrorMessage(utf8String);
                } catch (UnsupportedEncodingException ex) {
                    throw new RuntimeException(ex);
                }
                
            }
            e.printStackTrace();
        }
        return incResponce;
    }

    /**
     * 制作授权
     * @param ukeyRequest
     * @return
     */
    @PostMapping("/generate-password")
    @ApiOperation(value = "生成U盾口令")
    public IncResponce generatePassword(@RequestBody UkeyRequest ukeyRequest){
        IncResponce incResponce = new IncResponce();
        try {
            if (ObjectUtils.isEmpty(ukeyRequest) || 
                    StringUtils.isEmpty(ukeyRequest.getCode()) ||
                    StringUtils.isEmpty(ukeyRequest.getUserAccount()) || 
                    StringUtils.isEmpty(ukeyRequest.getUserPassword())) {
                throw new ParameterException("请求参数异常！");
            }
            LisenceUserEntity lisenceUserEntity = new LisenceUserEntity();
            lisenceUserEntity.setUserAccount(ukeyRequest.getUserAccount());
            LisenceUserEntity entity = lisenceUserMapper.selectEntityByAccountAndPassword(lisenceUserEntity);
            if (ObjectUtils.isEmpty(entity)){
                throw new BaseException("账号密码错误！");
            }
            String decrypt = EncipherUtils.decrypt(entity.getPassword());
            if (!ukeyRequest.getUserPassword().equals(decrypt)){
                throw new BaseException("账号密码错误！");
            }
            if (!(UserRoleEnum.PRODUCE_LEADER.getCode().equalsIgnoreCase(entity.getUserRole())||
                    UserRoleEnum.PRODUCE_MANAGER.getCode().equalsIgnoreCase(entity.getUserRole()))){
                throw new BaseException("账号无权限！");
            }
            String password = ukeyService.generatePassword(ukeyRequest.getCode(), ukeyRequest.getUserAccount());
            incResponce.setCode(0);
            incResponce.setData(password);
        } catch (Exception e) {
            incResponce.setCode(-1);
            if (StringUtils.isNotEmpty(e.getMessage())){
                try {
                    byte[] utf8Bytes  = e.getMessage().getBytes("GB2312");
                    String utf8String = new String(utf8Bytes, "GB2312");
                    incResponce.setErrorMessage(utf8String);
                } catch (UnsupportedEncodingException ex) {
                    throw new RuntimeException(ex);
                }
            }
            e.printStackTrace();
        }
        return incResponce;
    }

    /**
     * 变更制作状态
     * @param ukeyRequest
     * @return
     */
    @PostMapping("/update-status")
    @ApiOperation(value = "变更U盾状态")
    public IncResponce updateStatus(@RequestBody UkeyRequest ukeyRequest){
        IncResponce incResponce = new IncResponce();
        try {
            if (ObjectUtils.isEmpty(ukeyRequest) ||
                    StringUtils.isEmpty(ukeyRequest.getCode()) ||
                    StringUtils.isEmpty(ukeyRequest.getUserAccount()) ||
                    StringUtils.isEmpty(ukeyRequest.getUserPassword())) {
                throw new ParameterException("请求参数异常！");
            }
            LisenceUserEntity lisenceUserEntity = new LisenceUserEntity();
            lisenceUserEntity.setUserAccount(ukeyRequest.getUserAccount());
            LisenceUserEntity entity = lisenceUserMapper.selectEntityByAccountAndPassword(lisenceUserEntity);
            if (ObjectUtils.isEmpty(entity)){
                throw new BaseException("账号密码错误！");
            }
            String decrypt = EncipherUtils.decrypt(entity.getPassword());
            if (!ukeyRequest.getUserPassword().equals(decrypt)){
                throw new BaseException("账号密码错误！");
            }
            if (!(UserRoleEnum.PRODUCE_LEADER.getCode().equalsIgnoreCase(entity.getUserRole())||
                    UserRoleEnum.PRODUCE_MANAGER.getCode().equalsIgnoreCase(entity.getUserRole()))){
                throw new BaseException("账号无权限！");
            }
            ukeyService.updateStatus(ukeyRequest.getCode(), ukeyRequest.getStatus());
            incResponce.setCode(0);
        } catch (Exception e) {
            incResponce.setCode(-1);
            if (StringUtils.isNotEmpty(e.getMessage())){
                try {
                    byte[] utf8Bytes  = e.getMessage().getBytes("GB2312");
                    String utf8String = new String(utf8Bytes, "GB2312");
                    incResponce.setErrorMessage(utf8String);
                } catch (UnsupportedEncodingException ex) {
                    throw new RuntimeException(ex);
                }
            }
            e.printStackTrace();
        }
        return incResponce;
    }

}
